
import 'package:flutter/material.dart';
import 'package:flutter_1911/pages/classification_page.dart';
import 'package:flutter_1911/plugin/as_manager.dart';
import 'package:flutter_1911/until/nav_until.dart';


//语言界面
class SpeakPage extends StatefulWidget {

  @override
  _SpeakPageState createState() => _SpeakPageState();
}

class _SpeakPageState extends State<SpeakPage>
    with  SingleTickerProviderStateMixin{
    String speakTips ='说话';
    String speakReuslt='';
    late Animation<double> animation;
    late AnimationController controller;

    @override
  void initState() {
   controller= AnimationController(
     vsync: this,duration: Duration(microseconds: 1000));
    animation =CurvedAnimation(parent: controller, curve: Curves.easeIn)
    ..addStatusListener((status) {
      if(status ==AnimationStatus.completed){
        controller.reverse();
      } else if(status ==AnimationStatus.dismissed){
        controller.forward();
      }
    });
    super.initState();
  }
  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: EdgeInsets.all(30),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[_topitem(),_bottomitem()],
          ),
        ),
      ),
    );
  }
  //点击搜索的按钮改变文字 为 识别中
    _speakStart(){
      controller.forward();
      setState(() {
        speakTips='识别中';
      });
      AsrManager.start().then((text){
        if(text?.isNotEmpty ?? false){
          setState(() {
            speakReuslt =text!;
          });
          Navigator.pop(context);
          NavigatorUtil.push(
              context,
              ClassificationPage(
                keyword: speakReuslt,
              ));
          print('------'+text!);
        }
      }).catchError((e){
        print('----------'+e.toString());
      });
    }
    //语言顶部的文字
    _spekstop(){
      setState(() {
        speakTips='长按说话';
      });
      controller.reset();
      controller.stop();
      AsrManager.stop();
    }
    //顶部的item设置
    _topitem(){
      return Column(
        children: <Widget>[
          Padding(
              padding: EdgeInsets.fromLTRB(0, 30, 0, 30),
            child: Text('可以这样说话',
            style: TextStyle(fontSize: 15,color: Colors.greenAccent))),
          Text('谷歌\n北京\n迪士尼',
            textAlign:TextAlign.center,
           style: TextStyle(
              fontSize: 15,
              color: Colors.grey,)),
          Padding(padding:
            EdgeInsets.all(20),
            child: Text(
              speakReuslt,
              style: TextStyle(color: Colors.blue),
            ),
          )
        ],
      );
    }
    _bottomitem(){
      return FractionallySizedBox(
        widthFactor: 1,
        child: Stack(
          children: <Widget>[
            GestureDetector(
              onTapDown: (e){
                _speakStart();
              },
              onTapUp: (e){
                _spekstop();
              },
              onTapCancel: () {
                _spekstop();
              },
              child: Center(
                child: Column(
                  children: <Widget>[
                    Padding(padding: EdgeInsets.all(10),
                    child: Text(
                      speakTips,
                      style: TextStyle(color:Colors.blue, fontSize: 12),
                    ),
                    ),
                    Stack(
                      children: <Widget>[
                        Container(
                          //占坑，避免动画执行过程中导致父布局大小变得
                          height: MIC_SIZE,
                          width: MIC_SIZE,
                        ),
                        Center(
                          child: AnimatedMic(
                            animation,
                          ),
                        )
                      ],
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      );
    }
}
const double MIC_SIZE = 80;

class AnimatedMic extends AnimatedWidget{

  static final _opacityTween = Tween<double>(begin: 1, end: 0.5);
  static final _sizeTween = Tween<double>(begin: MIC_SIZE, end: MIC_SIZE - 20);

  AnimatedMic(Animation<double> animation, {Key? key})
      : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable as Animation<double>;
    return Opacity(
      opacity: _opacityTween.evaluate(animation),
      child: Container(
        height: _sizeTween.evaluate(animation),
        width: _sizeTween.evaluate(animation),
        decoration: BoxDecoration(
          color: Colors.blue,
          borderRadius: BorderRadius.circular(MIC_SIZE / 2),
        ),
        child: Icon(
          Icons.mic,
          color: Colors.white,
          size: 30,
        ),
      ),
    );
  }

}